package tuathal.snake;

import java.util.ArrayList;

public class Snake {
	protected final static int SLOW = 500;
	protected final static int MID = 300;
	protected final static int FAST = 100;
	protected final static int LEFT = 1;
	protected final static int UP = 2;
	protected final static int RIGHT = 3;
	protected final static int DOWN = 4;
	protected final static int RUNNING = 1;
	protected final static int PAUSE = 2;
	protected final static int GAMEOVER = 3;

	public boolean isRun;
	protected ArrayList<Node> body;
	protected Node food;
	protected int derection;
	protected int score;
	protected int status;
	protected int speed;

	public Snake() {
		isRun = false;
		score = 0;
		speed = Snake.SLOW;
		status = Snake.PAUSE;
		derection = Snake.RIGHT;
		body = new ArrayList<Node>();
		body.add(new Node(20, 40));
		body.add(new Node(40, 40));
		body.add(new Node(60, 40));
		makeFood();
	}

	public void move() {
		if (isEaten()) {
			score += 10;
		} else if (isCollsion()) {
			isRun = false;
			status = Snake.GAMEOVER;
		} else {
			Node head = body.get(0);
			body.remove(body.size() - 1);
			if (derection == Snake.RIGHT) {
				body.add(0, new Node(head.x + Node.W, head.y));
			}
			if (derection == Snake.LEFT) {
				body.add(0, new Node(head.x - Node.W, head.y));
			}
			if (derection == Snake.UP) {
				body.add(0, new Node(head.x, head.y - Node.H));
			}
			if (derection == Snake.DOWN) {
				body.add(0, new Node(head.x, head.y + Node.H));
			}
		}
	}

	public void makeFood() {
		boolean isBodyNode = true;
		int i = 0;
		int h = 0;
		int z = 0;
		while (isBodyNode) {
			h = ((int) (Math.random() * 15)) * Node.H;
			z = ((int) (Math.random() * 20)) * Node.W;
			for (i = 0; i < body.size(); i++) {
				if (body.get(i).x == h && body.get(i).y == z) {
					break;
				}
			}
			if (i >= body.size()) {
				isBodyNode = false;
			} else {
				isBodyNode = true;
			}

		}
		food = new Node(h, z);
	}

	public boolean isEaten() {
		Node head = body.get(0);
		if (derection == Snake.RIGHT && head.x == food.x - Node.W
				&& head.y == food.y) {
			body.add(0, food);
			makeFood();
			return true;
		}
		if (derection == Snake.LEFT && head.x == food.x + Node.W
				&& head.y == food.y) {
			body.add(0, food);
			makeFood();
			return true;
		}
		if (derection == Snake.UP && head.x == food.x
				&& head.y == food.y + Node.H) {
			body.add(0, food);
			makeFood();
			return true;
		}
		if (derection == Snake.DOWN && head.x == food.x
				&& head.y == food.y - Node.H) {
			body.add(0, food);
			makeFood();
			return true;
		} else
			return false;
	}

	public boolean isCollsion() {
		Node head = body.get(0);
		boolean isP = false;
		if (head.x == 0 || head.x == 280 || head.y == 0 || head.y == 380) {
			return true;
		}
		for (int i = 3; i < body.size(); i++) {
			Node foods = body.get(i);
			if (derection == Snake.RIGHT && head.x == foods.x + Node.W
					&& head.y == foods.y) {
				isP = true;
				break;
			}
			if (derection == Snake.LEFT && head.x == foods.x - Node.W
					&& head.y == foods.y) {
				isP = true;
				break;
			}
			if (derection == Snake.UP && head.x == foods.x
					&& head.y == foods.y + Node.H) {
				isP = true;
				break;
			}
			if (derection == Snake.DOWN && head.x == foods.x
					&& head.y == foods.y - Node.H) {
				isP = true;
				break;
			}
		}
		return isP;
	}

	public void changeDerection(int newspeed) {
		if (derection % 2 != newspeed % 2) {
			derection = newspeed;
		}
	}
}
